import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/Login'
import Home from '@/components/Home'
import Users from '@/components/user/Users'
import Roles from '@/components/power/Roles'
import Rights from '@/components/power/Rights'
import Goods from '@/components/goods/Goods'
import Add from '@/components/goods/Add'
import Orders from '@/components/orders/Orders'
// 
Vue.use(Router)

const router = new Router({
        routes: [{
                path: '/',
                redirect: '/login'
            }, {
                path: '/login',
                name: 'Login',
                component: Login
            },
            {
                path: '/home',
                name: 'Home',
                component: Home,
                redirect: '/users',
                children: [
                    //用户列表
                    { path: '/users', component: Users },
                    //权限管理
                    { path: '/rights', component: Rights },
                    //角色列表
                    { path: '/roles', component: Roles },
                    //商品列表
                    { path: '/goods', component: Goods },
                    //订单列表
                    { path: '/orders', component: Orders },
                    //添加商品
                    { path: '/goods/add', component: Add },
                ]
            }
        ]
    })
    //挂载路由导航卫士
router.beforeEach((to, from, next) => {
    //to：将要访问的路径
    //from：代表从哪个路径跳转而来
    //newxt：放行  next（‘/login’）
    if (to.path === '/login') return next()
        //获取token
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/login')
    next()
})


// 暴露
export default router;